package com.hisan.freeride.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.view.View;

import com.hisan.freeride.R;
import com.hisan.freeride.common.base.AppConfig;
import com.hisan.freeride.common.base.BaseActivity;
import com.hisan.freeride.common.callback.DialogCallback;
import com.hisan.freeride.common.utils.CollectionUtils;
import com.hisan.freeride.common.utils.utils.MyHandlerUtil;
import com.hisan.freeride.common.view.MyLinearLayoutManager;
import com.hisan.freeride.databinding.RideuserBinding;
import com.hisan.freeride.home.adapter.RideUserdapter;
import com.hisan.freeride.home.model.RideUser;
import com.lzy.okgo.OkGo;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建时间 : 2017/6/8
 * 创建人：yangyingqi
 * 公司：嘉善和盛网络有限公司
 * 备注：常用乘坐人页面
 */
public class RideUserActivity extends BaseActivity<RideuserBinding> {
    private RideUserdapter rideUserdapter;
    private RideUser rideUser;
    private int istype=1;

    @Override
    protected int getLayoutId() {
        return R.layout.rideuser;
    }

    @Override
    protected void initView() {
        mBinding.setStateModel(mStateModel);
        rideUserdapter=new RideUserdapter(new ArrayList<>(0));
        mBinding.rv.setLayoutManager(new MyLinearLayoutManager(this));
        mBinding.rv.setAdapter(rideUserdapter);
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        dividerItemDecoration.setDrawable(getResources().getDrawable(R.drawable.shape_drive_line));
        mBinding.rv.addItemDecoration(dividerItemDecoration);
       Bundle bundle= getIntent().getExtras();
       if (!CollectionUtils.isNullOrEmpty(bundle)){
           istype=bundle.getInt("type");
       }
    }

    @Override
    protected void initEvent() {
        rideUserdapter.setButtonClickListener(data -> {
            if (!CollectionUtils.isNullOrEmpty(data)){
                rideUser = data;
                getmDialog("是否删除当前乘坐人?", "取消", "确定").show();
            }
        });
        mBinding.topBack.setOnClickListener(v -> finishActivity(true));
        rideUserdapter.setItemClickListener(data -> {
            if (istype==1){
                Bundle bundle=new Bundle();
                bundle.putInt("id",data.getId());
                startActivityForResult(RideUserListActivity.class,99,bundle,true);
            }

        });
        mBinding.slectName.setOnClickListener(v -> startActivityForResult(RideUserListActivity.class,99,null,true));
    }

    @Override
    protected void loadData(boolean isRefresh) {
        OkGo.<List<RideUser>>get(AppConfig.plist)
                .tag(this)
                .execute(new DialogCallback<List<RideUser>>(this) {
                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response<List<RideUser>> response) {
                        super.onSuccess(response);
                        if (!CollectionUtils.isNullOrEmpty(response.body())) {
                            if (isOk(response)){
                                showData();
                                if (response.body().size() >= 5) {
                                    mBinding.slectName.setVisibility(View.GONE);
                                } else {
                                    mBinding.slectName.setVisibility(View.VISIBLE);
                                }
                                if (!CollectionUtils.isNullOrEmpty(response.body()))
                                    rideUserdapter.setNewData(response.body());
                            }else {
                                showErrorView(response.code());
                            }
                        }else {
                            noData();
                        }
                    }

                    @Override
                    public void onError(com.lzy.okgo.model.Response<List<RideUser>> response) {
                        super.onError(response);
                        showErrorView(response.code());
                    }
                });
    }

    @Override
    public void onOk() {
        super.onOk();
        Delete();
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (!CollectionUtils.isNullOrEmpty(data)){
            if (resultCode==99){
                loadData(true);
            }
        }
    }


    //删除乘坐人
    private void Delete() {
        OkGo.delete(AppConfig.pdelete)
                .tag(this)
                .params("id",rideUser.getId())
                .execute(new DialogCallback<Object>(this) {
                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response<Object> response) {
                        super.onSuccess(response);
                        if (isOk(response)){
                            showToast("删除成功");
                            MyHandlerUtil.getHandler().postDelayed(() -> {
                                loadData(true);
                            },200);
                        }else {
                            showError(response);
                        }
                    }
                    @Override
                    public void onError(com.lzy.okgo.model.Response<Object> response) {
                        super.onError(response);
                        showError(response);
                    }
                });
    }
}
